package utils

import (
	"fmt"
	openapi "github.com/alibabacloud-go/darabonba-openapi/client"
	dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
	"github.com/alibabacloud-go/tea/tea"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
	sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20210111" // 引入sms
)


/**
 * 使用AK&SK初始化账号Client
 * @param accessKeyId
 * @param accessKeySecret
 * @return Client
 * @throws Exception
 */
func CreateClient (accessKeyId *string, accessKeySecret *string) (_result *dysmsapi20170525.Client, _err error) {
	config := &openapi.Config{
		// 您的AccessKey ID
		AccessKeyId: accessKeyId,
		// 您的AccessKey Secret
		AccessKeySecret: accessKeySecret,
	}
	// 访问的域名
	config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
	_result = &dysmsapi20170525.Client{}
	_result, _err = dysmsapi20170525.NewClient(config)
	return _result, _err
}

func SendMsg(args []*string)(_err error){
	client, _err := CreateClient(tea.String("LTAI5t7k4jL85L82UiMCn9jd"), tea.String("rrU8uJNzzOwZMSwSeg7VycXhwhNMm8"))
	if _err != nil {
		return _err
	}

	sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
		PhoneNumbers: tea.String("13459213269"),
		SignName: tea.String("用户注册验证码"),
		TemplateCode: tea.String("SMS_216670680"),
		TemplateParam: tea.String("{\"code\":\"1111\"}"),
	}
	// 复制代码运行请自行打印 API 的返回值
	data, _err := client.SendSms(sendSmsRequest)
	fmt.Println(data)
	if _err != nil {
		return _err
	}
	return _err
}

func SendTencentMsg(phone,code,typeID string) (error){
	credential := common.NewCredential(
		// os.Getenv("TENCENTCLOUD_SECRET_ID"),
		// os.Getenv("TENCENTCLOUD_SECRET_KEY"),
		"AKIDZw3uUnpa4HDtJFuIPFo67sLXS6TZOxFH",
		"mHVS8XhaHiupSotiFVUDfqkOlrgEWWGi",
	)
	cpf := profile.NewClientProfile()
	cpf.HttpProfile.ReqMethod = "POST"
	cpf.HttpProfile.Endpoint = "sms.tencentcloudapi.com"
	cpf.SignMethod = "HmacSHA1"
	client, _ := sms.NewClient(credential, "ap-guangzhou", cpf)
	request := sms.NewSendSmsRequest()
	request.SmsSdkAppId = common.StringPtr("1400434700")
	request.SignName = common.StringPtr("祝晨企业管理")
	request.SenderId = common.StringPtr("")
	request.SessionContext = common.StringPtr("xxx")
	request.ExtendCode = common.StringPtr("")
	request.TemplateParamSet = common.StringPtrs([]string{code})
	request.TemplateId = common.StringPtr(typeID)
	request.PhoneNumberSet = common.StringPtrs([]string{"+86"+phone})
	_, err := client.SendSms(request)
	// 处理异常
	if err, ok := err.(*errors.TencentCloudSDKError); ok {
		//fmt.Printf("An API error has returned: %s", err)
		return err
	}
	// 非SDK异常，直接失败。实际代码中可以加入其他的处理。
	if err != nil {
		return err
	}
	//b, _ := json.Marshal(response.Response)
	//// 打印返回的json字符串
	//fmt.Printf("%s", b)

	return nil
}